package Limit.Event.Easter;

import l1j.server.Config;
import l1j.server.server.ClientThread;
import l1j.server.server.PacketHandler;
import l1j.server.server.clientpackets.C_Chat;
import l1j.server.server.clientpackets.ClientBasePacket;
import l1j.server.server.datatables.CheckTable;
import l1j.server.server.datatables.ItemTable;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.model.Instance.L1SummonInstance;
import l1j.server.server.serverpackets.S_NPCTalkReturn;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.templates.L1Item;
import l1j.server.server.utils.Random;

public class E_Easter extends ClientBasePacket
{
	private static final String E_Easter = "[E] E_Easter";
	Event_EasterTable EE = Event_EasterTable.getInstance();
	int[][] Rank1_Item = EE.getItemArray1();
	int[][] Rank2_Item = EE.getItemArray2();
	int[][] Rank3_Item = EE.getItemArray3();
	int[][] Rank4_Item = EE.getItemArray4();
	
	public E_Easter(byte abyte0[], ClientThread client) throws Exception 
	{
		super(abyte0);
		int objid = readD();
		String s = readS();
		
		L1PcInstance pc = client.getActiveChar();
		L1Object obj = L1World.getInstance().findObject(objid);
		Event_EasterTicketTable easter = new Event_EasterTicketTable();
		
		String htmlid = "";
		String[] htmldata = null;
		
		int[] materials = null;
		int[] counts = null;
		int[] createitem = null;
		int[] createcount = null;
		
		if (((L1NpcInstance) obj).getNpcTemplate().get_npcId() == 901017 && Config.EasterStart)
		{
			PacketHandler.stop = true;
			if (obj != null) 
			{
				if (obj instanceof L1NpcInstance) 
				{
					L1NpcInstance npc = (L1NpcInstance) obj;
					int difflocx = Math.abs(pc.getX() - npc.getX());
					int difflocy = Math.abs(pc.getY() - npc.getY());
					
					if (!(obj instanceof L1PetInstance)
						&& !(obj instanceof L1SummonInstance)) 
					{					
						if ((difflocx > 3) || (difflocy > 3)) 
						{ // 3格以上的距離對話無效
							return;
							}
					}
					npc.onFinalAction(pc, s);
				}
			}
			
			if (((L1NpcInstance) obj).getNpcTemplate().get_npcId() == 901017)
			{
				String[] Data = new String[27];
				String[] Data2 = new String[8];
				
				if (s.equalsIgnoreCase("eventeaster"))
				{				
					if(pc.getInventory().checkItem(70117, 1)){	
						htmlid = "Easter_ST";
						Data[0] = "3332";
						int i = 0;
						
						for (i = 1; i < 27; i++)
							Data[i] = "3339";
					} 				
					else 				
					{
						pc.sendPackets(new S_SystemMessage("所需道具不足。"));
					}
					htmldata = Data;
				} 
				else if (s.equalsIgnoreCase("easterstart"))
				{
					if(pc.getInventory().checkItem(70117, 1))
					{	
						htmlid = "Easter_ST";
						gameLoop(pc, htmlid);
						
						for (int i = 0; i < 27; i++)
							Data[i] = "3339";
						
						for (int k = 2; k < 14; k++)
							Data[k] = "3342";
						
						Data[1] = "3341";
						htmldata = Data;
					} 
					else 
					{
						pc.sendPackets(new S_SystemMessage("所需道具不足。"));
					}
				} 
				else if (s.equalsIgnoreCase("change"))
				{
					materials = new int[] { 70500 };
					counts = new int[] { 10 };
					createitem = new int[] { 70117 };
					createcount = new int[] { 1 };
					
					if (pc.getInventory().checkItem(70500, 10))
					{
						easter.loadCount(99999);
						int Count = easter.getCount();
						easter.updateCount(99999, (Count+1));
						CheckTable.getInstance().updateInspectCount(70500, 10); // 更新愛心幣check table
					}
				} 
				else if (s.equalsIgnoreCase("eastertickets"))
				{
					htmlid = "Easter_MT";
					
					for (int i = 0; i < 8; i++)
						Data2[i] = "*";
					
					htmldata = Data2;
				} 
				else if (s.equalsIgnoreCase("easterclean"))
				{
					if (easter.countMarkTickets(pc.getId()) == 1)
					{
						easter.renewMarkTicket(pc.getId(), "*");
						htmlid = "Easter_MT";
						
						for (int i = 0; i < 8; i++)
							Data2[i] = "*";
						
						htmldata = Data2;
					} 
					else 
					{
						pc.sendPackets(new S_SystemMessage("遊戲異常，請與ＧＭ聯繫。"));
					}
				} 
				else if (s.equalsIgnoreCase("easterticketonly"))
				{
					if(pc.getInventory().checkItem(70118, 1))
					{
						pc.getInventory().consumeItem(70118, 1);
						
						if (easter.countMarkTickets(pc.getId()) == 0)
							easter.StoreMarkTicket(pc.getId(), pc.getName());
						
						htmlid = "Easter_MT";
	
						for (int i = 0; i < 8; i++)
							Data2[i] = "*";
						
						htmldata = Data2;
					} 
					else 
					{
						pc.sendPackets(new S_SystemMessage("所需道具不足。"));
					}
				} 
				else if (s.equalsIgnoreCase("finallytickets"))
				{
					easter.LoadMarkTicket(pc.getId());
					String enighth = easter.getEighthNum();
					
					if(!enighth.equalsIgnoreCase("*"))
					{
						String finallynum = finallynum(pc.getId(), easter);
						
						if (easter.countOYTickets(finallynum) == 0)
						{
							easter.StoreTickets(pc.getId(), pc.getName(), finallynum);
							easter.deleteMarkTicket(pc.getId());
							htmlid = "Easter_T";
							htmldata = easter.LoadTickets(pc.getId());
						} 
						else 
						{
							easter.renewMarkTicket(pc.getId(), "*");
							htmlid = "Easter_MT";
							
							for (int i = 0; i < 8; i++)
								Data2[i] = "*";
							
							htmldata = Data2;
							pc.sendPackets(new S_SystemMessage("此號碼已被使用，請重新選擇。"));
						}
					} 
					else 
					{
						pc.sendPackets(new S_SystemMessage("號碼選擇未完。"));
					}
				} 
				else 
				{
					String[] whichs = s.split("_");
					int which = Integer.parseInt(whichs[1]);
					
					if (s.equalsIgnoreCase("easterbutton_" + which))
					{
						if(pc.getInventory().checkItem(70117, 1))
						{	
							pc.getInventory().consumeItem(70117, 1);
							
							htmlid = "Easter_ST";
							int[] iteminfo = ItemInfo();
							int itemid = iteminfo[0];
							int itemcount = iteminfo[2];
							int imgid = iteminfo[3];
							int img[] = new int[6];
							int place[] = new int[12];
							int j = 0;
							
							gamebutton(pc, htmlid, which, imgid);
							
							easter.loadCount(itemid);
							int Count = easter.getCount();
							easter.updateCount(itemid, (Count+1));
							easter.loadCount(99998);
							Count = easter.getCount();
							easter.updateCount(99998, (Count+1));
							
							if (easter.countMarkTickets(pc.getId()) == 0)
								easter.StoreMarkTicket(pc.getId(), pc.getName());
							
							for (j = 0; j < 14; j++)
								Data[j] = "3339";
							
							place[0] = which+13;
							
							for (j = 1; j < 12; j++)
								place[j] = rankplace(place);
							
							Data[place[0]] = ""+(imgid+32);
							Data[place[1]] = ""+rndimg1(imgid);
							img[0] = imgid;
							
							for (j = 1; j < 3; j++)
								img[j] = rndimg2(img);
							
							Data[place[2]] = ""+img[1];
							Data[place[3]] = ""+img[2];
							
							for (j = 1; j < 4; j++)
								img[j] = rndimg3(img);
							
							Data[place[4]] = ""+img[1];
							Data[place[5]] = ""+img[2];
							Data[place[6]] = ""+img[3];
							
							for (j = 1; j < 6; j++)
								img[j] = rndimg4(img);
							
							Data[place[7]] = ""+img[1];
							Data[place[8]] = ""+img[2];
							Data[place[9]] = ""+img[3];
							Data[place[10]] = ""+img[4];
							Data[place[11]] = ""+img[5];;
							
							Data[1] = "3340";
							Data[26] = "3344";
							
							htmldata = Data;
							L1ItemInstance item = pc.getInventory().storeItem(itemid, itemcount);
							pc.sendPackets(new S_SystemMessage("獲得" + item.getName()));						
							int[] itemidlist = new int[16];
							
							for(j = 0; j < 8; j++)
								itemidlist[j] = Rank1_Item[j][0];
							
							for(j = 8; j < 16; j++)
								itemidlist[j] = Rank2_Item[j-8][0];
							
							for (j = 0; j < 16; j++)
							{
								if (itemid == itemidlist[j])
								{
									C_Chat.BroadCastToAll("\\fV" + "恭喜玩家【" + pc.getName()+ "】抽中了"+ item.getName());
								}
							}
						}
						else 
						{
							pc.sendPackets(new S_SystemMessage("所需道具不足。"));
						}	
					} 
					else if (s.equalsIgnoreCase("easterts_" + which))
					{
						easter.LoadMarkTicket(pc.getId());
						String first = easter.getFirstNum();
						String second = easter.getSecondNum();
						String third = easter.getThirdNum();
						String fourth = easter.getFourthNum();
						String fifth = easter.getFifthNum();
						String sixth = easter.getSixthNum();
						String serventh = easter.getSeventhNum();
						String enighth = easter.getEighthNum();
						
						if(first.equalsIgnoreCase("*"))
							easter.updateMarkTicket(pc.getId(), "firstnum", ""+which);
						
						 else if(second.equalsIgnoreCase("*"))
							easter.updateMarkTicket(pc.getId(), "secondnum", ""+which);
						
						 else if(third.equalsIgnoreCase("*"))
							easter.updateMarkTicket(pc.getId(), "thirdnum", ""+which);
						
						 else if(fourth.equalsIgnoreCase("*"))
							easter.updateMarkTicket(pc.getId(), "fourthnum", ""+which);
						
						 else if(fifth.equalsIgnoreCase("*"))
							easter.updateMarkTicket(pc.getId(), "fifthnum", ""+which);
						
						 else if(sixth.equalsIgnoreCase("*"))
							easter.updateMarkTicket(pc.getId(), "sixthnum", ""+which);
						
						 else if(serventh.equalsIgnoreCase("*"))
							easter.updateMarkTicket(pc.getId(), "seventhnum", ""+which);
						
						 else if(enighth.equalsIgnoreCase("*"))
							easter.updateMarkTicket(pc.getId(), "eighthnum", ""+which);
						
						htmlid = "Easter_MT";
						
						Data2[0] = easter.getFirstNum();
						Data2[1] = easter.getSecondNum();
						Data2[2] = easter.getThirdNum();
						Data2[3] = easter.getFourthNum();
						Data2[4] = easter.getFifthNum();
						Data2[5] = easter.getSixthNum();
						Data2[6] = easter.getSeventhNum();
						Data2[7] = easter.getEighthNum();
						
						htmldata = Data2;
					}
				}	
			}
			if (createitem != null) 
			{
				boolean isCreate = true;
				if (materials != null) 
				{
					for (int j = 0; j < materials.length; j++) 
					{
						if (!pc.getInventory().checkItemNotEquipped(materials[j],
								counts[j]))
						{
							L1Item temp = ItemTable.getInstance().getTemplate(materials[j]);
							pc.sendPackets(new S_ServerMessage(337, temp.getName()));
							isCreate = false;
						}
					}
				}
	
				if (isCreate) 
				{
					int create_count = 0; 
					int create_weight = 0;
					for (int k = 0; k < createitem.length; k++) 
					{
						if ((createitem[k] > 0) && (createcount[k] > 0)) 
						{
							L1Item temp = ItemTable.getInstance().getTemplate(createitem[k]);
							if (temp != null) 
							{
								if (temp.isStackable()) 
								{
									if (!pc.getInventory().checkItem(createitem[k])) 
									{
										create_count += 1;
									}
								} 
								else 
								{
									create_count += createcount[k];
								}
								create_weight += temp.getWeight() * createcount[k] / 1000;
							}
						}
					}
	
					if (pc.getInventory().getSize() + create_count > 180) 
					{
						pc.sendPackets(new S_ServerMessage(263));
						return;
					}
	
					if (pc.getMaxWeight() < pc.getInventory().getWeight() + create_weight) 
					{
						pc.sendPackets(new S_ServerMessage(82));
						return;
					}
	
					if (materials != null)
					{
						for (int j = 0; j < materials.length; j++) 
						{
							pc.getInventory().consumeItem(materials[j], counts[j]);
						}
					}
					for (int k = 0; k < createitem.length; k++) 
					{
						if ((createitem[k] > 0) && (createcount[k] > 0)) 
						{
							L1ItemInstance item = pc.getInventory().storeItem(createitem[k], createcount[k]);
							if (item != null) 
							{
								String itemName = ItemTable.getInstance().getTemplate(createitem[k]).getName();
								String createrName = "";
								
								if (obj instanceof L1NpcInstance) 
								{
									createrName = ((L1NpcInstance) obj).getNpcTemplate().get_name();
								}
								if (createcount[k] > 1)
									pc.sendPackets(new S_ServerMessage(143,createrName, itemName + " ("+ createcount[k] + ")"));
								
								else
									pc.sendPackets(new S_ServerMessage(143,createrName, itemName));
							}
						}
					}
				}
			}
	
			if (htmlid != null) 
			{
				pc.sendPackets(new S_NPCTalkReturn(objid, htmlid, htmldata));
			}
		}
	}
	
	public String finallynum(int charid, Event_EasterTicketTable easter) 
	{
		String num = "";
		easter.LoadMarkTicket(charid);
		num = easter.getFirstNum() + easter.getSecondNum() + easter.getThirdNum() + easter.getFourthNum() + easter.getFifthNum() + easter.getSixthNum() + easter.getSeventhNum() + easter.getEighthNum();
		return num;
	}
	
	public void gameLoop(L1PcInstance pc, String html)
	{
		String[] gamedata = new String[27];
		int j = 0;
		
		for (j = 0; j < 27; j++)
			gamedata[j] = "3339";
		
		try 
		{
			for (int i = 0; i < 3; i++)
			{
				gamedata[1] = "3333";
				pc.sendPackets(new S_NPCTalkReturn(pc.getId(), html, gamedata));
				Thread.sleep(200);
				
				gamedata[1] = "3334";
				pc.sendPackets(new S_NPCTalkReturn(pc.getId(), html, gamedata));
				Thread.sleep(200);
			}
			gamedata[1] = "3335";
			pc.sendPackets(new S_NPCTalkReturn(pc.getId(), html, gamedata));
			Thread.sleep(300);
			
			gamedata[1] = "3336";
			pc.sendPackets(new S_NPCTalkReturn(pc.getId(), html, gamedata));
			Thread.sleep(300);
			
			gamedata[1] = "3337";
			pc.sendPackets(new S_NPCTalkReturn(pc.getId(), html, gamedata));
			Thread.sleep(300);
			
			gamedata[1] = "3338";
			pc.sendPackets(new S_NPCTalkReturn(pc.getId(), html, gamedata));
			Thread.sleep(1000);
			
		} 
		catch (InterruptedException e) 
		{
			e.printStackTrace();
		}
	}
	
	public void gamebutton(L1PcInstance pc, String html, int button, int img)
	{
		String[] gamedata = new String[27];
		int buttonnum = button + 13;
		int j = 0;
		
		for (j = 0; j < 14; j++)
			gamedata[j] = "3339";
		
		for (j = 14; j < 2６; j++)
			gamedata[j] = "3342";
		
		gamedata[26] = "3339";
		gamedata[1] = "3340";
		pc.sendPackets(new S_NPCTalkReturn(pc.getId(), html, gamedata));
		
		try 
		{
			for (int i = 0; i < 2; i++)
			{
				gamedata[buttonnum] = "3343";
				pc.sendPackets(new S_NPCTalkReturn(pc.getId(), html, gamedata));
				Thread.sleep(200);
				
				gamedata[buttonnum] = "3342";
				pc.sendPackets(new S_NPCTalkReturn(pc.getId(), html, gamedata));
				Thread.sleep(200);
			}
			gamedata[buttonnum] = "3343";
			pc.sendPackets(new S_NPCTalkReturn(pc.getId(), html, gamedata));
			Thread.sleep(200);
			
			for (int i = 0; i < 3; i++)
			{
				gamedata[buttonnum] = ""+img;
				pc.sendPackets(new S_NPCTalkReturn(pc.getId(), html, gamedata));
				Thread.sleep(200);
				
				gamedata[buttonnum] = ""+(img+32);
				pc.sendPackets(new S_NPCTalkReturn(pc.getId(), html, gamedata));
				Thread.sleep(200);
			}
		} 
		catch (InterruptedException e) 
		{
			e.printStackTrace();
		}
		pc.sendPackets(new S_NPCTalkReturn(pc.getId(), html, gamedata));
	}
	
	public int[] ItemInfo()
	{
		int itemInfo[] = new int[4];		
		int rankRnd = Random.nextInt(100) + 1;
		int rankSecondRnd = Random.nextInt(100) + 1;
		int item_Change_Rnd = Random.nextInt(100) + 1;
		int itemRnd = Random.nextInt(8);
		int item_Chance; 
		
		if (rankRnd <= Config.Rank1)
		{
			if (rankSecondRnd <= Config.Srnd)
			{
				itemInfo = Rank1_Item[itemRnd]; //itemId
				item_Chance = Rank1_Item[itemRnd][1]; //itemChance
				
				if (item_Change_Rnd >  item_Chance)
				{
					itemInfo = Rank3_Item[itemRnd];
				}
			} 
			else
			{
				itemInfo = Rank3_Item[itemRnd];
			}
		} 
		else if (rankRnd <= Config.Rank2)
		{
			if (rankSecondRnd <= Config.Srnd)
			{
				itemInfo = Rank2_Item[itemRnd];
				item_Chance = Rank2_Item[itemRnd][1];
				
				if (item_Change_Rnd >  item_Chance)
				{
					itemInfo = Rank4_Item[itemRnd];
				}
			} 
			else 
			{
				itemInfo = Rank4_Item[itemRnd];
			}
		} 		
		else if (rankRnd <= Config.Rank3)
		{
			itemInfo = Rank3_Item[itemRnd];
		} 
		else if (rankRnd > Config.Rank3 && rankRnd <= 100)
		{
			itemInfo = Rank4_Item[itemRnd];
		}
		return itemInfo;
	}
	
	public int rankplace(int[] place)
	{
		int k = Random.nextInt(12) + 14;
		
		for(int i = 0; i < 12; i++)
		{
			if(k == place[i])
			{
				return rankplace(place);
			}
		}
		return k;
	}
	
	public int rndimg1(int img)
	{
		int k = Random.nextInt(8);
		int imgrank[] = new int[8];
		
		for (int i = 0; i < 8; i++)
		{
			imgrank[i] = Rank1_Item[i][3];
		}
		
		int imgnum = imgrank[k];
		
		if(img == imgrank[k])
		{
			return rndimg1(img);
		}
		
		return imgnum;
	}
	
	public int rndimg2(int[] img)
	{
		int k = Random.nextInt(8);
		int imgrank[] = new int[8];
		
		for (int i = 0; i < 8; i++)
			imgrank[i] = Rank2_Item[i][3];
		
		int imgnum = imgrank[k];
		
		for (int j = 0; j < 2 ; j++)
		{
			if(img[j] == imgrank[k])
			{
				return rndimg2(img);
			}
		}
		
		return imgnum;
	}
	
	public int rndimg3(int[] img)
	{
		int k = Random.nextInt(8);
		int imgrank[] = new int[8];
		
		for (int i = 0; i < 8; i++)
			imgrank[i] = Rank3_Item[i][3];
		
		int imgnum = imgrank[k];
		
		for (int j = 0; j < 3 ; j++)
		{
			if(img[j] == imgrank[k])
			{
				return rndimg3(img);
			}
		}
		
		return imgnum;
	}
	
	public int rndimg4(int[] img)
	{
		int k = Random.nextInt(8);
		int imgrank[] = new int[8];
		
		for (int i = 0; i < 8; i++)
			imgrank[i] = Rank4_Item[i][3];
		
		int imgnum = imgrank[k];
		
		for (int j = 0; j < 5 ; j++)
		{
			if(img[j] == imgrank[k])
			{
				return rndimg4(img);
			}
		}
		
		return imgnum;
	}
	
	public String getType() 
	{
		return E_Easter;
	}
}
